<template>
  <!--引用表格-->
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <!--插槽:table标题-->
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 发起申请</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:delete-outlined"></Icon>
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <a-button
        >批量操作
          <Icon icon="ant-design:down-outlined"></Icon>
        </a-button>
      </a-dropdown>
    </template>
    <!--操作栏-->
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!-- 发起申请区域 -->
  <myApplyDrawer  @register="registerDrawer"  @success="reload"  />
  <!--编辑-->
  <itemModal @register="registerModal" @success="reload"></itemModal>
   <!--提交-->
  <submitModal @register="registerSubModal" @success="reload"></submitModal>
  <!--撤回-->
  <cancelModal @register="registerCancelbModal" @success="reload"></cancelModal>
  <!--历史记录-->
  <historyModal @register="registerHistoryModal"></historyModal>
  <!--表单详情-->
  <itemSpModal @register="registerSpModal" @success="reload"></itemSpModal>
</template>

<script lang="ts" name="system-dict" setup>
  //ts语法
  import { ref, computed, unref } from 'vue';
  import { BasicTable, TableAction } from '/src/components/Table';
  import { useDrawer } from '/src/components/Drawer';
  import { useModal } from '/src/components/Modal';
  import { useMessage } from '/src/hooks/web/useMessage';
  import { removeAuthCache, setAuthCache } from '/src/utils/auth';
  import { columns, searchFormSchema } from './myApply.data';
  import myApplyDrawer from './components/myApplyDrawer.vue'
  import { useListPage } from '/@/hooks/system/useListPage';
  import { list,delByIds} from './myApply.api';
  import itemModal from './components/itemModal.vue';
  import submitModal from './components/submitModal.vue';
  import cancelModal from './components/cancelModal.vue';
  import historyModal from './components/historyModal.vue';
  import itemSpModal from '../dsApply/components/itemSpModal.vue';

  const [registerModal, {openModal:addOpenModal}] = useModal();
  const [registerSubModal, {openModal:subOpenModal}] = useModal();
  const [registerCancelbModal, {openModal:cancelOpenModal}] = useModal();
  
  const [registerHistoryModal, {openModal:historyOpenModal}] = useModal();

  const [registerSpModal, {openModal:spOpenModal}] = useModal();
  //字典配置drawer
  const [registerDrawer, { openDrawer:addOpenDrawer }] = useDrawer();

  const { createMessage } = useMessage();
  //字典model
  // const [registerModal, { openModal }] = useModal();

  // 列表页面公共参数、方法
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    designScope: 'dict-template',
    tableProps: {
      title: '我的申请',
      api: list,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 300,
      },
    },
    //update-begin---author:wangshuai ---date:20220616  for：[issues/I5AMDD]导入/导出功能，操作后提示没有传递 export.url/import.url 参数------------
    // exportConfig: {
    //   name: '数据字典列表',
    //   url: getExportUrl,
    // },
    // importConfig: {
    //   url: getImportUrl,
    // },
    //update-end---author:wangshuai ---date:20220616  for：[issues/I5AMDD]导入/导出功能，操作后提示没有传递 export.url/import.url 参数--------------
  });

  //注册table数据
  const [registerTable, { reload, updateTableDataRecord }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 新增事件
   */
  function handleCreate() {
    addOpenDrawer(true, {
      isUpdate: false,
    });
  }
  //提交
  function handleApply(record: Recordable){
    subOpenModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
  }
  /**
   * 编辑事件
   */
  async function handleEdit(record: Recordable) {
    addOpenModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
  }
  /**
   * 详情
   */
  // async function handleDetail(record) {
  //   openModal(true, {
  //     record,
  //     isUpdate: true,
  //   });
  // }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await delByIds({ ids: record.id }, reload);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    // await batchDeleteDict({ ids: selectedRowKeys.value }, reload);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    // if (isUpdate) {
    //   updateTableDataRecord(values.id, values);
    // } else {
      console.log("回掉更新！")
      reload();
    // }
  }
  //撤回
  function handleSpCancel(record: Recordable){
    cancelOpenModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     }); 
  }
  //查看进度
  function handleHistory(record: Recordable){
    historyOpenModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }
  //表单数据
  function handleDetail(record: Recordable){
    spOpenModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
}
  /**
   * 操作栏
   */
  function getTableAction(record) {
    let arrActionList=[];
    let tempStatus=record.status;
      if(tempStatus==0){
        arrActionList.push( {label: '提交',onClick: handleApply.bind(null, record)},)
        arrActionList.push( {label: '编辑',onClick: handleEdit.bind(null, record)},)
        arrActionList.push({ label: '删除',popConfirm: { title: '确定删除吗?',confirm: handleDelete.bind(null, record), },});
      }else if(tempStatus==1){
        if(record.procInstStatus==1){
          arrActionList.push( {label: '撤回',onClick: handleSpCancel.bind(null, record)},)
          arrActionList.push( {label: '查看进度',onClick: handleHistory.bind(null, record)},)
        }
        arrActionList.push( {label: '表单数据',onClick: handleDetail.bind(null, record)},)

      }else if((tempStatus==2 && record.result==3) || tempStatus==3){
        arrActionList.push( {label: '重新申请',onClick: handleApply.bind(null, record)},)
        arrActionList.push( {label: '编辑',onClick: handleEdit.bind(null, record)},)
        arrActionList.push( {label: '审批历史',onClick: handleHistory.bind(null, record)},)
      }else{
        arrActionList.push( {label: '表单数据',onClick: handleDetail.bind(null, record)},)
        arrActionList.push( {label: '审批历史',onClick: handleHistory.bind(null, record)},)
      }
    return arrActionList;
    // return [
    //   {
    //     label: '编辑',
    //     onClick: handleEdit.bind(null, record),
    //   },
    //   {
    //     label: '删除',
    //     popConfirm: {
    //       title: '确定删除吗?',
    //       confirm: handleDelete.bind(null, record),
    //     },
    //   },
    // ];
  }
</script>

<style scoped></style>
